﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using ECSE6770.Models;

namespace ECSE6770.Controllers
{
    public abstract class BaseController : Controller
    {
        public BaseController()
        {
            Db = ContextManager.CurrentDb;
        }

        public EntitiesContainer Db { get; private set; }

        public bool IsSuperAdmin { get; private set; }

        public bool IsAdmin
        {
            get { return User.IsInRole("Admin"); }
        }

        public string SuperAdminUserName
        {
            get { return ConfigurationManager.AppSettings["SuperAdminName"]; }
        }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            IsSuperAdmin = User.Identity.IsAuthenticated && (User.Identity.Name == SuperAdminUserName);
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //store some helper variables
            ViewBag.IsSuperAdmin = this.IsSuperAdmin;

            base.OnActionExecuting(filterContext);
        }

        public ActionResult Success(string title, string text)
        {
            return Success(title, text, null, null);
        }

        public ActionResult Success(string title, string text, string linkText, string linkUrl)
        {
            return View("Success", new SuccessInfo(title, text, linkText, linkUrl));
        }

        public ActionResult ToIndex()
        {
            return RedirectToAction("Index");
        }

        public void BccEmailBlast(string from, string subject, string bodyHtml, IEnumerable<string> bcc)
        {
            MailMessage msg = new MailMessage { From = new MailAddress(from), Subject = subject, Body = bodyHtml, IsBodyHtml = true };
            foreach (string address in bcc)
                msg.Bcc.Add(address);
            new SmtpClient().Send(msg);
        }
    }
}
